<?php

namespace app\backstage\controller;

use app\backstage\service\AdService;
use app\backstage\service\AdPositionService;

class Ad extends Base
{
	protected $datas;
	protected $title;
	protected $table = 'ad';

	/**
	 * 轮播图列表
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-14T10:49:43+0800
	 * @return   [type]                   [description]
	 */
	public function index()
	{
		$this->datas = $this->params;
		$this->title = '轮播图列表';
		$whereMap = [];
		if (isset($this->datas['name'])) {
			if (!empty($this->datas['name'])) {
				$whereMap[] = ['ad.name', 'like', '%' . $this->datas['name'] . '%'];
			}
			if (!empty($this->datas['ad_position_id'])) {
				$whereMap[] = ['ad.ad_position_id', '=', $this->datas['ad_position_id']];
			}
		}
		$list = AdService::getList($whereMap, 5);
		$AdPositionList = AdPositionService::getList([], 'id,name');
		$this->assign([
			'title' => $this->title,
			'list' => $list,
			'AdPosition' => $AdPositionList
		]);
		return $this->fetch();
	}

	/**
	 * 添加修改
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-14T10:49:59+0800
	 * @return   [type]                   [description]
	 */
	public function form()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$data = [
				'ad_position_id' => $this->datas['ad_position_id'],
				'media_type' => '',
				'name' => $this->datas['name'],
				'link' => $this->datas['link'], 
				'image_url' => '',
				'content' => $this->datas['content'],
				'end_time' => ''
			];
			if (isset($this->datas['id'])) {
				$data['update_time'] = time();
				$result = AdService::update('id', $this->datas['id'], $data);
			} else {
				$data['create_time'] = time();
				$result = AdService::add($data);
			}
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$id = input('id', '', 'trim');
		$data = [];
		if (!empty($id)) {
			$data = AdService::getInfo('id', $id);
		}
		$postitonList = AdPositionService::getList([], 'id,name');
		$this->assign([
			'postitonList' => $postitonList,
			'data' => $data
		]);
		return $this->fetch();
	}

	/**
	 * 禁用启用
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-14T10:50:15+0800
	 * @return   [type]                   [description]
	 */
	public function enabled()
	{
		$this->datas = $this->params;
		$id = $this->datas['id'];
		$status = $this->datas['status'];
		$result = AdService::update('id', $id, ['enabled' => $status, 'update_time' => time()]);
		if (!$result) {
			$this->error('失败');
		}
		$this->success('成功');
	}

	public function delAd()
	{
		$id = input('id', '', 'trim');
		$result = AdService::del($id);
		if (!$result) {
			$this->error('失败');
		}
		$this->success('成功');
	}
	

	/**
	 * 广告位置列表
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-10T22:48:10+0800
	 * @return   [type]                   [description]
	 */
	public function position()
	{
		$this->title = '广告位置';
		$list = AdPositionService::pageList([], 'id asc');
		$this->assign([
			'title' => $this->title,
			'list' => $list
		]);
		return $this->fetch();
	}

	/**
	 * 添加修改
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-14T10:50:30+0800
	 * @return   [type]                   [description]
	 */
	public function postitonForm()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$data = [
				'name' => $this->datas['name'],
				'height' => $this->datas['height'],
				'width' => $this->datas['width'],
				'desc' => $this->datas['desc']
			];
			if (isset($this->datas['id'])) {
				$data['update_time'] = time();
				$result = AdPositionService::update('id', $this->datas['id'], $data);
			} else {
				$data['create_time'] = time();
				$result = AdPositionService::add($data);
			}
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$id = input('id', '', 'trim');
		$data = [];
		if (!empty($id)) {
			$data = AdPositionService::getInfo('id', $id);
		}
		$this->assign(['data' => $data]);
		return $this->fetch();
	}

	/**
	 * 删除广告位置
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-14T10:50:44+0800
	 * @return   [type]                   [description]
	 */
	public function delAdPostiton()
	{
		$id = input('id', '', 'trim');
		$result = AdPositionService::del($id);
		if (!$result) {
			$this->error('失败');
		}
		$this->success('成功');
	}

	/**
	 * 上传图片
	 * @Author   fanzhongxing
	 * @DateTime 2020-06-14T10:51:09+0800
	 * @return   [type]                   [description]
	 */
	public function uploadimg()
	{
		$path = 'static'. DS . 'upload' . DS . 'avatar';
		$size = 58400*100;
		$type = 'jpg,png,gif,jpeg';
		$uploadResult = uploadFile('file', $path, $size, $type);
		if (!$uploadResult[0]) {
			$this->error($uploadResult[1]);
		}
		$this->success('上传成功', '', $uploadResult[1]);
	}
}